package com.gopay.scheduler.job;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.common.domain.cps.wallet.Wallet01200Cron01614VO;
import com.gopay.common.util.PwdControlUtil;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.CallCPSHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.List;

/**
 * 手机钱包01614交易（一分钱快捷支付）退款
 * Created by along on 2016/7/4. All Right reserved
 */
public class Wallet01614RefundJob extends GopayBaseJob{

    // 日志
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "cpsGeneralMainOrderDAO")
    private CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderDAO;

    @Resource(name = "callCPSHelper")
    private CallCPSHelper callCPSHelper;

    @Override
    protected void doJobInternal() {
        List<CpsGenMainOrder> cpsGenMainOrderList = cpsGeneralMainOrderDAO.
                getMainOrders(TxnStaCode.TXN_STA_CD_20000.value, IntTxnCd._01614.value);

        // 调用CPS
        Wallet01200Cron01614VO tranVo;
        CPSResponse response;
        CpsGenMainOrder main;
        for (int i =0;i<cpsGenMainOrderList.size();i++) {
            main = cpsGenMainOrderList.get(i);

            tranVo = buildCallCpsParamVo(main);

            logger.error("01614交易发起退款的订单号------GopayOrderId=" + main.getGopayOrderId());
            try {
                // 调用cps
                response = callCPSHelper.callByVOTicket(tranVo);
                if (!ProcCodeConstants.PROC_CODE_100S1000.equals(response.getRespCode())) {
                    logger.error("------调用cps返回异常  response=" + response.getRespCode());
                }
            } catch (Exception e) {
                // 吃掉所有异常
                logger.error("callByVOTicket Error", e);
            }
        }
    }

    public Wallet01200Cron01614VO buildCallCpsParamVo(CpsGenMainOrder main) {
        Wallet01200Cron01614VO tranVo = new Wallet01200Cron01614VO();
        // 封装参数Vo
        // 付款客户ID
        tranVo.setRefundCustId(main.getPayCustId());
        // 付款用户ID
        tranVo.setRefundUserId(main.getUserId());

        tranVo.setRefundAmt(main.getTotalOrderAmt());
        tranVo.setOrderId(main.getGopayOrderId());
        tranVo.setOriginOrderId(main.getGopayOrderId());

        return tranVo;
    }
}
